<?php

namespace app\admin\service;

use app\admin\model\Abnormal;
use app\admin\model\City;
use app\admin\model\Disease;
use app\admin\model\Unit;
use app\admin\model\User;

/**
 * 道路交通-病害管理
 */
class DiseaseService extends BaseService
{
    public function __construct()
    {
        $this->model = new Disease;
        $this->name ='DL';
    }

    public function lists(array $param)
    {
        $query = $this->model->where('mark', 1)->with(
            [
                'unit'     => function ($query) {
                    $query->field('id, unit_name');
                },
                'abnormal' => function ($query) {
                    $query->field('id, name');
                },
                'user'     => function ($query) {
                    $query->field('id, realname');
                }
            ]
        );
        if (!empty($param['operation_no'])) {
            $query->where('operation_no', 'like', '%' . $param['operation_no'] . '%');
        }
        if (!empty($param['organization_id'])) {
            $query->where('organization_id', $param['organization_id']);
        }
        if (!empty($param['operation_category'])) {
            $query->where('operation_category', $param['operation_category']);
        }
        if (!empty($param['exception_category'])) {
            $query->where('exception_category', $param['exception_category']);
        }
        if (!empty($param['area_id'])) {
            $query->where('area_id', end($param['area_id']));
        }
        if (isset($param['organization_show'])) {
            $query->where('organization_show', $param['organization_show']);
        }
        if (isset($param['penalty'])) {
            $query->where('penalty', $param['penalty']);
        }
        if (!empty($param['create_user'])) {
            $userIds = User::where('name', 'like', '%' . $param['create_user'] . '%')->column('id');
            $query->whereIn('create_user', $userIds);
        }
        if (!empty($param['date'])) {
            $query->where('create_time', 'between', array_map('strtotime', $param['date']));
        }
        if (!empty($param['lnglat'])) {
            $query->where('lng', 'between', [$param['lnglat'][0] - 0.0004, $param['lnglat'][0] +  0.0004]);
            $query->where('lat', 'between', [$param['lnglat'][1] - 0.0004, $param['lnglat'][1] +  0.0004]);
        }
        $data = [
            'msg'   => '操作成功',
            'code'  => 0,
            'data'  => [],
            'count' => (clone $query)->count('id'),
        ];
        if ($data['count']) {
            $data['data'] = $query->order('id', 'desc')
                ->page(PAGE, PERPAGE)
                ->select();

            foreach ($data['data'] as &$val) {
                $val['area']                    = $val['address'];
                $val['organization_name']       = $val['unit']['unit_name'];
                $val['exception_category_name'] = $val['abnormal']['name'];
                $val['create_user']             = $val['user']['realname'];
                $val['operation_category_name'] = $val['operation_category_name'];
                $val['images']  = $val['images'] ? json_decode($val['images'], true) : [];
            }
        }

        return $data;
    }

    public function info()
    {
        $id              = request()->param('id', 0);
        $info            = $this->model->getInfo($id);
        $info['images']  = $info['images'] ? json_decode($info['images'], true) : [];
        $info['area_id'] = [
            (substr($info['area_id'], 0, 2) . '0000'),
            (substr($info['area_id'], 0, 4) . '00'),
            (string)$info['area_id']
        ];

        return message("操作成功", true, $info);
    }

    public function edit()
    {
        $data = request()->param();
        $data['area_id'] = end($data['area_id']);
        $data['images']  = json_encode($data['images']);

        return parent::edit($data);
    }

    public function setShow($id)
    {
        if (is_array($id)) {
            $query = Disease::where('id', 'in', $id);
        } else {
            $query = Disease::where('id', $id);
        }
        $query->update(['organization_show' => 1]);

        return message();
    }
}

